import Api from "../common/api";
import httpManager from "../common/http";

export enum LoginType {
  Guest = "tourist",
  Phone = "phone",
  WeChat = "app_wx",
  QQ = "app_qq",
  Apple = "apple",
}

const logger = {
  d: console.log,
  e: console.error,
};

class ApiService {
  private static instance: ApiService;

  public static getInstance(): ApiService {
    if (!ApiService.instance) {
      ApiService.instance = new ApiService();
    }
    return ApiService.instance;
  }

  private __getURL(path: string): string {
    return `${Api.BASE_URL}${path}`;
  }

  public async login(
    loginType: LoginType,
    id: string,
    pwd: string
  ): Promise<BaseResponse | undefined> {
    const params = {
      loginType: loginType,
      openid: id,
      pwd: pwd,
      identity: 0,
      extraData: { ref: "Android" },
    };

    try {
      const response: any = await httpManager.post(
        this.__getURL(Api.LOGIN),
        params
      );
      if (!response || !response.data) {
        throw new Error("No response or invalid data from server");
      }
      if (response.data.code === 0) {
        return response.data.data;
      } else {
        throw new Error(response.data.msg || "Unknown error occurred");
      }
    } catch (error: any) {
      // 打印错误日志
      logger.e(error);

      // 根据 error 的类型抛出具体的错误信息
      if (error.response && error.response.data) {
        throw new Error(error.response.data.msg || "Server error");
      } else if (error.message) {
        throw new Error(error.message || "请求失败");
      } else {
        throw new Error("请求失败");
      }
    }
  }

  public async getUserInfo(uid: string): Promise<BaseResponse | undefined> {
    const url = `${this.__getURL(Api.GET_USER_INFO)}${uid}`;
    return await this.__get(url);
  }

  public async getHomeTagList(): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.TAG_LIST);
    return await this.__get(url);
  }

  public async getTagPipeList(
    tagId: string,
    page: number
  ): Promise<BaseResponse | undefined> {
    const url = this.__getURL(`${Api.TAG_PIPE_LIST}${tagId}`);
    const params = { pageNo: page };
    return await this.__get(url, params);
  }

  public async getSearchHotWordList(): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.SEARCH_HOT_WORD_LIST);
    return await this.__get(url);
  }

  public async getRecommendPipeList(): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.PIPE_RECOMMEND_LIST);
    return await this.__get(url);
  }  

  public async searchPipeList(text: string, page: number): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.SEARCH_PIPE_LIST);
    const params = { pageNo: page, name: text };
    return await this.__get(url, params);
  }  

  public async searchCartoonList(text: string, page: number): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.SEARCH_CARTOON_LIST);
    const params = { pageNo: page, name: text };
    return await this.__get(url, params);
  }  

  public async searchUserList(text: string, page: number): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.SEARCH_USER_LIST);
    const params = { pageNo: page, name: text };
    return await this.__get(url, params);
  }  

  public async getCartoonTabId(): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.TAB_CARTOON);
    return await this.__get(url);
  }

  public async getCartoonList(tabId: string, page: number): Promise<BaseResponse | undefined> {
    const url = `${this.__getURL(Api.TAB_CARTOON)}/${tabId}`;
    const params = { pageNo: page };
    return await this.__get(url, params);
  }

  public async getRecommendCartoons(): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.RCMD_FOLLOW_CARTOON);
    return await this.__get(url);
  }

  public async getUserPipeList(uid: string): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.GET_USER_PIPE_LIST);
    const params = { uid: uid };
    return await this.__get(url, params);
  }

  public async getUserCartoonList(uid: string): Promise<BaseResponse | undefined> {
    const url = this.__getURL(Api.GET_USER_CARTOON_LIST);
    const params = { uid: uid };
    return await this.__get(url, params);
  }

  private async __get(
    url: string,
    params?: any
  ): Promise<BaseResponse | undefined> {
    try {
      const response: any = await httpManager.get(url, params);
      if (response.data.code === 0) {
        return response.data;
      } else {
        throw new Error("获取数据失败");
      }
    } catch (error) {
      throw new Error("获取数据失败");
    }
  }

  private async __post(
    url: string,
    params?: any
  ): Promise<BaseResponse | undefined> {
    try {
      const response: any = await httpManager.post(url, params);
      if (response.data.code === 0) {
        return response.data;
      } else {
        throw new Error("获取数据失败");
      }
    } catch (error) {
      throw new Error("获取数据失败");
    }
  }
}

const apiService = ApiService.getInstance();

export default apiService;
